package com.example.gpstest.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.appcompat.app.AppCompatActivity;

import com.example.gpstest.DataBean.GPSSharedPreference;
import com.example.gpstest.R;
import com.example.gpstest.Utils.GPSConst;
import com.example.gpstest.Utils.ParametersPackage;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ParameterSetActivity extends AppCompatActivity {

    @BindView(R.id.editTestCount)
    EditText editTestCount;
    @BindView(R.id.editTimeSpan)
    EditText editTimeSpan;
    @BindView(R.id.editTTFF)
    EditText editTTFF;
    @BindView(R.id.editTrueLatitude)
    EditText editTrueLatitude;
    @BindView(R.id.editTrueLongitude)
    EditText editTrueLongitude;
    @BindView(R.id.editPrecisionLimit)
    EditText editPrecisionLimit;
    @BindView(R.id.radioBtn_cold)
    RadioButton radioBtnCold;
    @BindView(R.id.radioBtn_warm)
    RadioButton radioBtnWarm;
    @BindView(R.id.radioGrp_mode)
    RadioGroup radioGrpMode;
    @BindView(R.id.cbTime)
    CheckBox cbTime;
    @BindView(R.id.cbPosition)
    CheckBox cbPosition;
    @BindView(R.id.cbEphemeris)
    CheckBox cbEphemeris;
    @BindView(R.id.cbAlmanac)
    CheckBox cbAlmanac;
    @BindView(R.id.cblono_utc)
    CheckBox cblonoUtc;
    @BindView(R.id.cbHealth)
    CheckBox cbHealth;
    @BindView(R.id.cbDgps)
    CheckBox cbDgps;
    @BindView(R.id.cbStr)
    CheckBox cbStr;
    @BindView(R.id.radioBtn_GPS)
    RadioButton radioBtnGPS;
    @BindView(R.id.radioBtn_CELL)
    RadioButton radioBtnCELL;
    @BindView(R.id.radioBtn_WIFI)
    RadioButton radioBtnWIFI;
    @BindView(R.id.radioGrp_way)
    RadioGroup radioGrpWay;
    @BindView(R.id.btnConfirmSetting)
    Button btnConfirmSetting;
    @BindView(R.id.btnCancelSetting)
    Button btnCancelSetting;

    private ParametersPackage pkg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_parameter_set);
        ButterKnife.bind(this);
        pkg = new ParametersPackage();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initializeParameters();
    }

    @OnClick({R.id.radioBtn_cold, R.id.radioBtn_warm, R.id.radioGrp_mode, R.id.cbTime, R.id.cbPosition, R.id.cbEphemeris, R.id.cbAlmanac, R.id.cblono_utc, R.id.cbHealth, R.id.cbDgps, R.id.cbStr, R.id.radioBtn_GPS, R.id.radioBtn_CELL, R.id.radioBtn_WIFI, R.id.radioGrp_way, R.id.btnConfirmSetting, R.id.btnCancelSetting})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.radioBtn_cold:
                setAidDateEnable(false);
                break;
            case R.id.radioBtn_warm:
                setAidDateEnable(true);
                break;
            case R.id.radioGrp_mode:
                break;
            case R.id.cbTime:
                break;
            case R.id.cbPosition:
                break;
            case R.id.cbEphemeris:
                break;
            case R.id.cbAlmanac:
                break;
            case R.id.cblono_utc:
                break;
            case R.id.cbHealth:
                break;
            case R.id.cbDgps:
                break;
            case R.id.cbStr:
                break;
            case R.id.radioBtn_GPS:

                break;
            case R.id.radioBtn_CELL:
                break;
            case R.id.radioBtn_WIFI:
                break;
            case R.id.radioGrp_way:
                break;
            case R.id.btnConfirmSetting:
                saveParameters();
                finish();
                break;
            case R.id.btnCancelSetting:
                finish();
                break;
        }
    }
    public void saveParameters() {
        int policy = 0;
        int mode = -1;
        int way = -1;
        GPSSharedPreference.setPreference((Context) this, GPSConst.KEY_TEST_TIME, Integer.parseInt(editTestCount.getText().toString()));
        GPSSharedPreference.setPreference((Context) this, GPSConst.KEY_TIME_SPAN, Integer.parseInt(editTimeSpan.getText().toString()));
        GPSSharedPreference.setPreference((Context) this, GPSConst.KEY_TTFF, Integer.parseInt(editTTFF.getText().toString()));
        GPSSharedPreference.setPreference((Context) this, GPSConst.KEY_TRUE_LATITUDE, editTrueLatitude.getText().toString());
        GPSSharedPreference.setPreference((Context) this, GPSConst.KEY_TRUE_LONGITUDE, editTrueLongitude.getText().toString());
        GPSSharedPreference.setPreference((Context) this, GPSConst.KEY_PRECISION_LIMIT, editPrecisionLimit.getText().toString());

        GPSSharedPreference.setPreference((Context) this, GPSConst.KEY_POLICY, policy);
        if (this.radioBtnCold.isChecked()) {
            mode = 0;
        } else if (this.radioBtnWarm.isChecked()) {
            mode = 1;
            saveAidDate();
        }
        GPSSharedPreference.setPreference((Context) this, GPSConst.KEY_MODE, mode);
        if (this.radioBtnGPS.isChecked()) {
            way = 0;
        } else if (this.radioBtnCELL.isChecked()) {
            way = 1;
        } else if (this.radioBtnWIFI.isChecked()) {
            way = 2;
        }
        GPSSharedPreference.setPreference((Context) this, GPSConst.KEY_WAY, way);
    }

    public void saveAidDate() {
        GPSSharedPreference.setPreference((Context) this, GPSConst.KEY_WARM_TIME, Boolean.valueOf(this.cbTime.isChecked()));
        GPSSharedPreference.setPreference((Context) this, GPSConst.KEY_WARM_POSITION, Boolean.valueOf(this.cbPosition.isChecked()));
        GPSSharedPreference.setPreference((Context) this, GPSConst.KEY_WARM_EPHEMERIS, Boolean.valueOf(this.cbEphemeris.isChecked()));
        GPSSharedPreference.setPreference((Context) this, GPSConst.KEY_WARM_ALMANAC, Boolean.valueOf(this.cbAlmanac.isChecked()));
        GPSSharedPreference.setPreference((Context) this, GPSConst.KEY_WARM_LONO_UTS, Boolean.valueOf(this.cblonoUtc.isChecked()));
        GPSSharedPreference.setPreference((Context) this, GPSConst.KEY_WARM_HEALTH, Boolean.valueOf(this.cbHealth.isChecked()));
        GPSSharedPreference.setPreference((Context) this, GPSConst.KEY_WARM_DGPS, Boolean.valueOf(this.cbDgps.isChecked()));
        GPSSharedPreference.setPreference((Context) this, GPSConst.KEY_WARM_STR, Boolean.valueOf(this.cbStr.isChecked()));
    }

    public void initializeParameters() {
        getInitialValue();
        this.editTestCount.setText(String.valueOf(this.pkg.testTime));
        this.editTimeSpan.setText(String.valueOf(this.pkg.timeSpan));
        this.editTTFF.setText(String.valueOf(this.pkg.ttff));
        this.editTrueLatitude.setText(String.valueOf(this.pkg.trueLatitude));
        this.editTrueLongitude.setText(String.valueOf(this.pkg.trueLongitude));
        this.editPrecisionLimit.setText(String.valueOf(this.pkg.precisionLimit));

        if (this.pkg.fixMode == 0) {
            this.radioBtnCold.setChecked(true);
            setAidDateEnable(false);
        } else if (1 == this.pkg.fixMode) {
            this.radioBtnWarm.setChecked(true);
            setAidDateEnable(true);
            setAidDateCheck();
        }
        if (this.pkg.fixWay == 0) {
            this.radioBtnGPS.setChecked(true);
        } else if (1 == this.pkg.fixWay) {
            this.radioBtnCELL.setChecked(true);
        } else if (2 == this.pkg.fixWay) {
            this.radioBtnWIFI.setChecked(true);
        }
    }

    public void getInitialValue() {
        this.pkg.testTime = GPSSharedPreference.getPreference((Context) this, GPSConst.KEY_TEST_TIME, this.pkg.testTime);
        this.pkg.updatePeriod = GPSSharedPreference.getPreference((Context) this, GPSConst.KEY_UPDATE_PERIOD, this.pkg.updatePeriod);
        this.pkg.timeSpan = GPSSharedPreference.getPreference((Context) this, GPSConst.KEY_TIME_SPAN, this.pkg.timeSpan);
        this.pkg.ttff = GPSSharedPreference.getPreference((Context) this, GPSConst.KEY_TTFF, this.pkg.ttff);
        this.pkg.fixTime = GPSSharedPreference.getPreference((Context) this, GPSConst.KEY_FIX_TIME, this.pkg.fixTime);
        this.pkg.trueLatitude = Double.valueOf(GPSSharedPreference.getPreference((Context) this, GPSConst.KEY_TRUE_LATITUDE, String.valueOf(this.pkg.trueLatitude))).doubleValue();
        this.pkg.trueLongitude = Double.valueOf(GPSSharedPreference.getPreference((Context) this, GPSConst.KEY_TRUE_LONGITUDE, String.valueOf(this.pkg.trueLongitude))).doubleValue();
        this.pkg.precisionLimit = Double.valueOf(GPSSharedPreference.getPreference((Context) this, GPSConst.KEY_PRECISION_LIMIT, String.valueOf(this.pkg.precisionLimit))).doubleValue();
        this.pkg.fixPolicy = GPSSharedPreference.getPreference((Context) this, GPSConst.KEY_POLICY, this.pkg.fixPolicy);
        this.pkg.fixMode = GPSSharedPreference.getPreference((Context) this, GPSConst.KEY_MODE, this.pkg.fixMode);
        this.pkg.fixWay = GPSSharedPreference.getPreference((Context) this, GPSConst.KEY_WAY, this.pkg.fixWay);
        this.pkg.warmTime = GPSSharedPreference.getPreference((Context) this, GPSConst.KEY_WARM_TIME, Boolean.valueOf(this.pkg.warmTime));
        this.pkg.warmPosition = GPSSharedPreference.getPreference((Context) this, GPSConst.KEY_WARM_POSITION, Boolean.valueOf(this.pkg.warmPosition));
        this.pkg.warmEphemeris = GPSSharedPreference.getPreference((Context) this, GPSConst.KEY_WARM_EPHEMERIS, Boolean.valueOf(this.pkg.warmEphemeris));
        this.pkg.warmAlmanac = GPSSharedPreference.getPreference((Context) this, GPSConst.KEY_WARM_ALMANAC, Boolean.valueOf(this.pkg.warmAlmanac));
        this.pkg.warmLono_uts = GPSSharedPreference.getPreference((Context) this, GPSConst.KEY_WARM_LONO_UTS, Boolean.valueOf(this.pkg.warmLono_uts));
        this.pkg.warmHealth = GPSSharedPreference.getPreference((Context) this, GPSConst.KEY_WARM_HEALTH, Boolean.valueOf(this.pkg.warmHealth));
        this.pkg.warmDgps = GPSSharedPreference.getPreference((Context) this, GPSConst.KEY_WARM_DGPS, Boolean.valueOf(this.pkg.warmDgps));
        this.pkg.warmStr = GPSSharedPreference.getPreference((Context) this, GPSConst.KEY_WARM_STR, Boolean.valueOf(this.pkg.warmStr));
    }
    public void setAidDateEnable(boolean status) {
        this.cbTime.setEnabled(status);
        this.cbPosition.setEnabled(status);
        this.cbEphemeris.setEnabled(status);
        this.cbAlmanac.setEnabled(status);
        this.cblonoUtc.setEnabled(status);
        this.cbHealth.setEnabled(status);
        this.cbDgps.setEnabled(status);
        this.cbStr.setEnabled(status);
    }
    public void setAidDateCheck() {
        this.cbTime.setChecked(this.pkg.warmTime);
        this.cbPosition.setChecked(this.pkg.warmPosition);
        this.cbEphemeris.setChecked(this.pkg.warmEphemeris);
        this.cbAlmanac.setChecked(this.pkg.warmAlmanac);
        this.cblonoUtc.setChecked(this.pkg.warmLono_uts);
        this.cbHealth.setChecked(this.pkg.warmHealth);
        this.cbDgps.setChecked(this.pkg.warmDgps);
        this.cbStr.setChecked(this.pkg.warmStr);
    }

}
